package com.kugou.helloworld;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;

import com.kugou.test.helper.JniCoverage;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import static com.kugou.test.helper.TestHelper.*;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //JniCoverage.getInstance().init("/sdcard/a");
        //ServiceUtil.bindToService(this.getBaseContext());

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onButtonDebug();
            }
        });

        findViewById(R.id.new_activity).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent openActivityIntent = new Intent(MainActivity.this,NextActivity.class);
                startActivity(openActivityIntent);
            }
        });
    }

    private void onButtonDebug() {
        //JniCoverage.getInstance().getString1("ddd");
        //JniCoverage.getInstance().flush_cov();
        //ServiceUtil.getServiceRunTime();
//        new AsyncTask<Object, Object, Object>() {
//            @Override
//            protected String doInBackground(Object... arg0) {
//                return null;
//            }
//        }.execute();
        sendRequestWithHttpURLConnection();
    }


    private void sendRequestWithHttpURLConnection(){
        new Thread(new Runnable() {//开启xian
            @Override
            public void run() {
                HttpURLConnection connection = null;
                BufferedReader reader = null;
                try{
                    URL url = new URL("http://www.baidu.com");//新建URL
                    connection = (HttpURLConnection)url.openConnection();//发起网络请求
                    connection.setRequestMethod("GET");//请求方式
                    connection.setConnectTimeout(8000);//连接最大时间
                    connection.setReadTimeout(8000);//读取最大时间
                    InputStream in = connection.getInputStream();
                    reader = new BufferedReader(new InputStreamReader(in));//写入reader
                    StringBuilder response = new StringBuilder();
                    String line;
                    while((line = reader.readLine()) != null){
                        response.append(line);
                    }
                    // showResponse(response.toString());//更新ui
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    if(reader != null){
                        try{
                            reader.close();
                        }catch (IOException e){
                            e.printStackTrace();
                        }
                    }
                    if(connection != null){
                        connection.disconnect();
                    }
                }
            }
        }).start();
    }
}